import { createRouter, createWebHashHistory } from 'vue-router'
import { getCookie } from '@/utils/cookie'
import { ref } from 'vue'

import HomeView from '@/views/Home/index.vue'
import LoginView from '@/views/Login/index.vue'
import NotFound from '@/views/NotFound/index.vue'

import FoundMusic from '@/views/Container/components/foundMusic.vue'
import PlayFriend from '@/views/Container/components/playFriends.vue'
import Video from '@/views/Container/components/video.vue'
import Concern from '@/views/Container/components/concern.vue'
import Broadcast from '@/views/Container/components/broadcast.vue'
import SelfFM from '@/views/Container/components/selfFM.vue'
import SongsList from '@/components/songsList.vue'
// 引入排行榜组件
import Ranking from '@/views/Container/components/Ranking/index.vue'
// 引入歌单组件
import PlayList from '@/views/Container/components/PlayList/index.vue'

import Recommend from '@/views/Container/components/foundMusic/recommend.vue'

const routes = [
  { path: '/', redirect: '/home/foundMusic/recommend' },
  { path: '/home', redirect: '/home/foundMusic/recommend' },
  { path: '/home/foundMusic', redirect: '/home/foundMusic/recommend' },
  {
    path: '/home', name: 'home', component: HomeView, children: [
      {
        path: 'foundMusic', name: 'foundMusic', component: FoundMusic, title: '发现音乐', focus: true, children: [
        { path: 'recommend', name: 'recommend', component: Recommend, title: '个性推荐' },
        { path: 'ranking', name: 'ranking', component: Ranking, title: '排行榜' },
        { path: 'playlist', name: 'playlist', component: PlayList, title: '歌单' },
        ]
      },
      { path: 'playFriends', name: 'playFriend', component: PlayFriend, title: '播客', focus:false },
      { path: 'video', name: 'video', component: Video, title: '视频', focus:false },
      { path: 'concern', name: 'concern', component: Concern, title: '关注', focus:false },
      { path: 'broadcast', name: 'broadcast', component: Broadcast, title: '直播', focus:false },
      { path: 'selfFM', name: 'selfFM', component: SelfFM, title: '私人FM', focus: false },
      { path: 'songList', name: 'songsList', component: SongsList },
    ] },
  { path: '/login', name: 'login', component: LoginView },
  { path: '/:pathMatch(.*)*', name: 'notfound', component: NotFound },
]

const router = createRouter({
  history: createWebHashHistory(import.meta.env.BASE_URL),
  routes
})

router.beforeEach((to) => {
  if (to.name !== 'login' && !getCookie('loginCookie')) {
    window.$message.error('登录已过期,请重新登录')
    router.push({ name: 'login' })
  }
})

export const list = routes[3].children


export default router
